import { routerRedux } from 'dva/router'
import { parse } from 'qs'
import config from 'config'
import { query } from 'services/borrow'
import { pageModel } from "./common"
import modelExtend from "dva-model-extend";
import { appendFileSync } from 'fs';
import { applyPrinting1, checkPrinting1, printingList1, getPrintImg1, workflowHistory1 } from 'services/printing'
import { getGzcFileDetails1 } from 'services/fileComponent'
import { userLis } from 'services/search'

const permissions = JSON.parse(sessionStorage.getItem("permissions"))

export default modelExtend(pageModel, {
	namespace: 'printing',
	state: {
		modalVisible: false,
		checkModalVisible: false,
		currentItem: {},
		params: "",
		goDetails: false,
		details: "",
		ImgList: "",
		userList: "",
		menu: permissions.indexOf("borrowManager:printing:apply") != -1 ? 0 : 2,  //判断权限如果是有申请权限则key为0，无则key为1
		sliderData: "",
		workflowList:"",
	},
	subscriptions: {
		setup({ dispatch, history }) {
			history.listen(location => {
				if (location.pathname === "/borrowManager/printing") {
					// dispatch({
					// 	type: "query",
					// 	payload: {
					// 		page: 1,
					// 		pageSize: 10,
					// 	}
					// });
					dispatch({
						type: "useLists"
					});
				}
			});
		}
	},
	effects: {
		*query({ payload }, { call, put }) {
			const params = {
				operatingState: 0,
				operatingType: 3
			}
			const data = yield call(query, { ...params, ...payload });
			if (data) {
				yield put({
					type: "querySuccess",
					payload: {
						list: data.page.list,
						pagination: {
							current: Number(payload.page) || 1,
							pageSize: Number(payload.pageSize) || 10,
							total: data.page.totalCount,
							totalCount: data.page.totalPage,
						}
					}
				})
			}
		},
		*applyList({ payload }, { call, put }) {
			const datas = {
				page: 1,
				pageSize: 10,
			}
			const data = yield call(printingList1, payload)
			// console.log(data)
			if (data) {
				yield put({
					type: "querySuccess",
					payload: {
						params: payload,
						list: data.page.list,
						pagination: {
							current: Number(payload.page) || 1,
							pageSize: Number(payload.pageSize) || 10,
							total: data.page.totalCount,
							totalCount: data.page.totalPage,
						}
					}
				})
			}
		},
		*apply({ payload }, { call, put }) {
			const values = payload
			const value = {}
			value.page = payload.page
			value.pageSize = payload.pageSize
			delete values.page
			delete values.pageSize
			const data = yield call(applyPrinting1, values)
			if (data) {
				yield put({
					type: 'hideModal'
				})
				yield put({
					type: 'query',
					payload: { ...value }
				})
			}
		},
		*checked({ payload }, { call, put }) {
			const data = yield call(checkPrinting1, payload)
			// console.log(data)
			if (data) {
				yield put({ type: 'hideCheckModal' })
				yield put({ type: 'applyList', payload: { status: 2, page: 1, pageSize: 10 } })
			}
		},
		*getAllImg({ payload }, { call, put }) {
			const data = yield call(getPrintImg1, payload)
			// console.log(data)
			if (data) {
				yield put({
					type: "getImgSuccess",
					payload: {
						ImgList: data.billList
					}
				})
				yield put({ type: "goDetails" })
			}
		},
		*showDet({ payload }, { call, put }) {
			const data = yield call(getGzcFileDetails1, payload)
			yield put({
				type: 'showDetailSuccess1',
				payload: {
					details: data,
				}
			})
		},
		*useLists({ payload }, { call, put }) {
			const data = yield call(userLis, payload);
			if (data.success) {
				yield put({
					type: "queryUserSuccess",
					payload: {
						userList: data.userList
					}
				});
			}
		},
		*showMenu({ payload }, { call, put }) {
			yield put({
				type: 'showMenuSuccess',
				payload: {
					menu: payload
				}
			})
		},
		//获取审批流程
		*getworkflowHistory({ payload }, { call, put }) {
			const data = yield call(workflowHistory1, payload);
			if (data.success) {
				yield put({
					type: "workflowSuccess",
					payload: {
						workflowList: data.applyList
					}
				})
			}
		},
	},
	reducers: {
		queryUserSuccess(state, action) {
			const { userList } = action.payload;
			return { ...state, userList };
		},
		getImgSuccess(state, action) {
			const { ImgList } = action.payload
			return {
				...state,
				ImgList
			}
		},
		showDetailSuccess1(state, action) {
			const { details } = action.payload
			return {
				...state,
				details,
				goDetails: true
			}
		},
		goList(state) {
			return {
				...state,
				goDetails: false
			}
		},
		showModal(state, { payload }) {
			return {
				...state,
				modalVisible: true,
				currentItem: payload.record
			}
		},
		hideModal(state, { payload }) {
			return {
				...state,
				modalVisible: false
			}
		},
		showCheckModal(state, { payload }) {
			return {
				...state,
				checkModalVisible: true,
				currentItem: payload.record
			}
		},
		hideCheckModal(state, { payload }) {
			return {
				...state,
				checkModalVisible: false
			}
		},
		showMenuSuccess(state, action) {
			const { menu } = action.payload
			return {
				...state,
				menu
			}
		},
		getSliderDetails(state, { payload }) {
			return {
				...state,
				sliderData: payload.record
			}
		},
		workflowSuccess(state,action){
			const { workflowList } = action.payload
			return{
			  ...state,
			  workflowList
			}
		  }
	}
});
